package cn.edu.cqu.fredyvia.forum.controller;

import cn.edu.cqu.fredyvia.forum.entity.User;
import cn.edu.cqu.fredyvia.forum.entity.UserInfo;
import cn.edu.cqu.fredyvia.forum.service.IUserInfoService;
import cn.edu.cqu.fredyvia.forum.service.IUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/userinfo")
public class UserInfoController extends BaseController {
    @Autowired
    public IUserInfoService userInfoService;

    @Autowired
    public IUserService userService;


    //文件上传
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userName = authentication.getName();
        User user = userService.getUserByUsername(userName);
        String upload = userInfoService.upload(user, file);
        if (upload != null)
            return AjaxResult.success(upload);
        return AjaxResult.error("unknown");
    }

    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(@RequestBody Map<String, Object> map) {
        logger.info(map.toString());
        Date birthday;
        try {
            birthday = new SimpleDateFormat("yyyy-MM-dd").parse((String)map.get("birthday"));
        } catch (ParseException e) {
            e.printStackTrace();
            return AjaxResult.error("失败了");
        }
        Integer sex = (Integer) map.get("sex");
        String description = (String) map.get("description");

        logger.info("‘‘’‘’‘’‘’‘’‘’‘’‘’’“”" + birthday + sex + description);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userName = authentication.getName();
        logger.info("userinfo/detail" + userName);

        UserInfo userInfo = new UserInfo();
        User user = userService.getUserByUsername(userName);
//        return userInfoService.getById();
        userInfo.setUserId(user.getId());
        userInfo.setUserdetail(description);
        userInfo.setAvatar(null);
        userInfo.setSex(sex);
        userInfo.setBirthday(birthday);
        // 这里不知道是否会填入
        userInfo.setScore(null);
        logger.info("userinfo/detail"+userInfo);

        return AjaxResult.success(userInfoService.saveOrUpdate(userInfo));
    }


    //获取用户
    @GetMapping("/detail")
    @ResponseBody
    public AjaxResult get() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userName = authentication.getName();
        logger.info("userinfo/detail" + userName);
        User user = userService.getUserByUsername(userName);
        logger.info("你妈死了：" + user.toString());
        UserInfo userInfo = userInfoService.getById(user.getId());
        if (userInfo == null) {
            userInfo = new UserInfo();
        }
        userInfo.setUsername(userName);
        userInfo.setUserId(user.getId());
//        logger.info((userInfo==null));
        return AjaxResult.success(userInfo);
    }
}
